package com.jinlejia.erp.ui.house;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.donkingliang.imageselector.utils.ImageSelector;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.FragmentApplyPropertiesBinding;
import com.jinlejia.erp.ui.common.ImageViewPagerActivity;
import com.jinlejia.erp.ui.common.MainActivity;
import com.jinlejia.erp.ui.common.ProgressDialog;
import com.jinlejia.erp.ui.common.SheetSelect;
import com.jinlejia.erp.ui.common.SheetWheelFloorHouse;
import com.jinlejia.erp.ui.house.prospect.bean.AreaBean;
import com.jinlejia.erp.ui.house.prospect.bean.DistrictStreetBean;
import com.jinlejia.erp.ui.house.prospect.bean.TradingAreaBean;
import com.jinlejia.erp.utils.DataFilter;
import com.jinlejia.erp.utils.UploadPictureOnly;
import com.jinlejia.erp.widget.image.UploadImageView;
import com.weight.CustomToast;
import com.weight.IconToast;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;
import top.zibin.luban.LubanZ;

/**
 * 申请楼盘和房间
 * A simple {@link Fragment} subclass.
 */
public class ApplyPropertiesFragment extends Fragment {

    private FragmentApplyPropertiesBinding binding;

    //梯户数据字典
    private ArrayList<Map<String,Object>> floordic = new ArrayList<>();
    private ArrayList<Map<String,Object>> housedic = new ArrayList<>();
    private List<TradingAreaBean> tradingAreaBeanslist = new ArrayList<>();

    //区县数据列表
    private List<AreaBean> areaBeanList = new ArrayList<>();

    //片区街道列表
    private List<DistrictStreetBean> districtStreetBeans = new ArrayList<>();

    private JSONObject jsonObject = new JSONObject();

    //房屋地址图片请求码
    private int HOUSE_ADDRESS = 100;

    //楼栋号图片请求码
    private int LOUDONG_NUMBER = 101;
    //单元号图片请求码
    private int DANYUAN_NUMBER = 102;
    //房门号图片请求码
    private int FANGMEN_NUMBER = 103;
    //所在单元全部房号报箱图片请求码
    private int HOUSE_NUMBER_ALL = 104;
    //楼盘名称图片请请求码
    private int LOUPAN_NAME = 105;
    private int isActual; //是否跃层

    //跃层参数
    private String yEnd;
    private String yStart;

    private ProgressDialog progressDialog;


    public ApplyPropertiesFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_apply_properties, container, false);
        binding = DataBindingUtil.bind(view);
        binding.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                ApplyPropertiesFragment.this.onCheckedChanged(group, checkedId);
            }
        });
        binding.setClickproperties(this);
        initRadio();
        getDiction();
        progressDialog = new ProgressDialog(getContext(), "请稍后...");
        binding.etLoudongnumber.setRawInputType(Configuration.KEYBOARD_QWERTY);
        binding.etLoudongDynumber.setRawInputType(Configuration.KEYBOARD_QWERTY);
        binding.etDanyuannumber.setRawInputType(Configuration.KEYBOARD_QWERTY);
        binding.etHousenumber.setRawInputType(Configuration.KEYBOARD_QWERTY);
        binding.etFloor.setRawInputType(Configuration.KEYBOARD_QWERTY);
        binding.etFloorAll.setRawInputType(Configuration.KEYBOARD_QWERTY);
        binding.uploadHouseAddress.setListener(new InnerListener(this,HOUSE_ADDRESS));
        binding.uploadLoupanName.setListener(new InnerListener(this,LOUPAN_NAME));
        binding.uploadLoudongNumber.setListener(new InnerListener(this,LOUDONG_NUMBER));
        binding.uploadDanyuanNumber.setListener(new InnerListener(this,DANYUAN_NUMBER));
        binding.uploadFangmennumber.setListener(new InnerListener(this,FANGMEN_NUMBER));
        binding.uploadAll.setListener(new InnerListener(this,HOUSE_NUMBER_ALL));
        return view;
    }

    /***
     * 是否有房本单选
     */

    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.rbHave) {
            binding.phoneHouseAddressLinear.setVisibility(View.VISIBLE);
            binding.btPullHave.setVisibility(View.VISIBLE);
            binding.phoneLine1.setVisibility(View.GONE);
            binding.phoneLine2.setVisibility(View.GONE);
            binding.btPullHavenot.setVisibility(View.GONE);
            binding.uploadFangmennumber.deleUrlMessage();
            binding.uploadAll.deleUrlMessage();
            binding.uploadDanyuanNumber.deleUrlMessage();
            binding.uploadLoudongNumber.deleUrlMessage();
            binding.uploadLoupanName.deleUrlMessage();
        } else if (checkedId == R.id.rbHavenot) {
            binding.phoneHouseAddressLinear.setVisibility(View.GONE);
            binding.btPullHave.setVisibility(View.GONE);
            binding.btPullHavenot.setVisibility(View.VISIBLE);
            binding.phoneLine1.setVisibility(View.VISIBLE);
            binding.phoneLine2.setVisibility(View.VISIBLE);
            binding.uploadHouseAddress.deleUrlMessage();
            binding.scrollview.post(new Runnable() {
                @Override
                public void run() {
                    binding.scrollview.fullScroll(ScrollView.FOCUS_DOWN);
                }
            });
        }
    }

    /***
     * 初始化界面显示
     */

    private void initRadio() {
        binding.rbHave.setChecked(true);
        binding.phoneHouseAddressLinear.setVisibility(View.VISIBLE);
        binding.btPullHave.setVisibility(View.VISIBLE);
        binding.phoneLine1.setVisibility(View.GONE);
        binding.phoneLine2.setVisibility(View.GONE);
        binding.btPullHavenot.setVisibility(View.GONE);

    }

    /***
     *跃层点击事件
     */
    public void yueFloorOnclick(View view) {
        yueFloorDialog();
    }



    /***
     *梯户点击事件
     */
    public void tihuOnclick(View view) {
        SheetWheelFloorHouse sheetWheelFloorHouse =new SheetWheelFloorHouse(getContext(),floordic,housedic);
        sheetWheelFloorHouse.show();
        sheetWheelFloorHouse.setOnSelectListener(new SheetWheelFloorHouse.OnSelectListener() {
            @Override
            public void select(Map<String, Integer> map) {
                jsonObject.putAll(map);
                binding.tvLaddeHousehold.setText(map.get("elevators")+"梯"+map.get("households")+"户");
            }
        });
    }

    /***
     *商圈点击事件
     */
    public void shangquanOnclick(View view) {
        if (TextUtils.isEmpty(binding.etHouseName.getText())) {
            CustomToast.showToast(getContext(), "请先输入楼盘名称", 1500);
            return;
        }

        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm!=null){
            imm.hideSoftInputFromWindow(getActivity().getWindow().getDecorView().getWindowToken(), 0);
        }
        binding.tvShangquan.setText(null);

        if (tradingAreaBeanslist != null) {
            ArrayList<Map<String, Object>> maps = new ArrayList<>();
            for (TradingAreaBean tradingAreaBean : tradingAreaBeanslist) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", String.valueOf(tradingAreaBean.getId()));
                map.put("valueName", tradingAreaBean.getBusinessName());
                maps.add(map);
            }

            final SheetSelect shangquanSelect = new SheetSelect(getContext(), "商圈", maps);
            shangquanSelect.show();
            shangquanSelect.setOnSelectListener(new SheetSelect.OnSelectListener() {
                @Override
                public void select(Object o) {
                    Map<String, Object> map = (Map<String, Object>) o;
                    String valueName = (String) map.get("valueName");
                    String id = (String) map.get("id");
                    binding.tvShangquan.setText(valueName);
                    binding.tvShangquan.setTag(id);
                    shangquanSelect.dismiss();
                }
            });

        }
    }

    /***
     *行政区点击事件
     */
    public void xingzhengquOnclick(View view) {
        if (TextUtils.isEmpty(binding.tvShangquan.getText())) {
            CustomToast.showToast(getContext(), "请先选择商圈", 1500);
            return;
        }
        binding.tvXingzhengqu.setText(null);
        binding.tvXingzhengqu.setTag(null);

        if (areaBeanList != null) {
            ArrayList<Map<String, Object>> maps = new ArrayList<>();
            for (AreaBean areaBean : areaBeanList) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", String.valueOf(areaBean.getCountyId()));
                map.put("valueName", areaBean.getCountyName());
                maps.add(map);
            }

            final SheetSelect xingzhengquSelect = new SheetSelect(getContext(), "行政区", maps);
            xingzhengquSelect.show();
            xingzhengquSelect.setOnSelectListener(new SheetSelect.OnSelectListener() {
                @Override
                public void select(Object o) {
                    Map<String, Object> map = (Map<String, Object>) o;
                    String valueName = (String) map.get("valueName");
                    String id = (String) map.get("id");
                    binding.tvXingzhengqu.setText(valueName);
                    binding.tvXingzhengqu.setTag(id);
                    xingzhengquSelect.dismiss();
                }
            });


        }
    }


    /***
     *片区街道点击事件
     */
    public void pianjieOnclick(View view){
        if (TextUtils.isEmpty(binding.tvXingzhengqu.getText())) {
            CustomToast.showToast(getContext(), "请先选择行政区", 1500);
            return;
        }
        binding.tvPianjie.setText(null);
        binding.tvPianjie.setTag(null);

        OkHttpUtils.get()
                .url(ApiConfig.DISTRICT_STREET.concat(binding.tvXingzhengqu.getTag().toString()))
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        CustomToast.showToast(getContext(),"网络连接失败",1500);
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(getContext(),response)){
                            JSONArray dataList = (JSONArray) response.get("data");
                            districtStreetBeans = JSON.parseArray(dataList.toString(),DistrictStreetBean.class);

                            new Thread(){
                                @Override
                                public void run() {
                                    super.run();
                                    getActivity().runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {

                                            if (districtStreetBeans != null) {
                                                ArrayList<Map<String, Object>> maps = new ArrayList<>();
                                                for (DistrictStreetBean districtStreetBean : districtStreetBeans) {
                                                    Map<String, Object> map = new HashMap<>();
                                                    map.put("id", String.valueOf(districtStreetBean.getAreaId()));
                                                    map.put("valueName", districtStreetBean.getAreaName());
                                                    maps.add(map);
                                                }
                                                final SheetSelect xingzhengquSelect = new SheetSelect(getContext(), "片区/街道", maps);
                                                xingzhengquSelect.show();
                                                xingzhengquSelect.setOnSelectListener(new SheetSelect.OnSelectListener() {
                                                    @Override
                                                    public void select(Object o) {
                                                        Map<String, Object> map = (Map<String, Object>) o;
                                                        String valueName = (String) map.get("valueName");
                                                        String id = (String) map.get("id");
                                                        binding.tvPianjie.setText(valueName);
                                                        binding.tvPianjie.setTag(id);
                                                        xingzhengquSelect.dismiss();
                                                    }
                                                });
                                            }
                                        }
                                    });
                                }
                            }.start();

                        }
                    }
                });
    }





    /***
     * 跃层弹出框
     */
    public void yueFloorDialog() {
        final Dialog bgSetDialog = new Dialog(getContext(), R.style.BottomDialogStyle);
        //填充对话框的布局
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_thermocline, null);
        //初始化控件
        final RadioButton rbYes = view.findViewById(R.id.rbYes);
        RadioButton rbNo = view.findViewById(R.id.rbNo);
        TextView tvSure = view.findViewById(R.id.tvSure);
        final EditText etYstart = view.findViewById(R.id.etYstart);
        final EditText etYend = view.findViewById(R.id.etYend);
        rbNo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                rbYes.setChecked(true);
                binding.tvYuefloor.setText("否");
                isActual = 2;
                bgSetDialog.dismiss();
            }
        });

        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 yStart= etYstart.getText().toString();
                yEnd = etYend.getText().toString();
                if (TextUtils.isEmpty(yStart)||TextUtils.isEmpty(yEnd)){
                    CustomToast.showToast(getContext(),"您还没有输入跃层信息",1500);
                }else{
                    if (Integer.parseInt(yStart)>= Integer.parseInt(yEnd)){
                        CustomToast.showToast(getContext(),"跃层请按从低到高的顺序填写！",1500);
                        return;
                    }
                    binding.tvYuefloor.setText(yStart + "跃" + yEnd);
                    isActual = 1;

                    // 隐藏软键盘
                    InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm!=null){
                        imm.hideSoftInputFromWindow(bgSetDialog.getWindow().getDecorView().getWindowToken(), 0);
                    }
                    bgSetDialog.dismiss();
                }
            }
        });
        //将布局设置给Dialog
        bgSetDialog.setContentView(view);
        //获取当前Activity所在的窗体
        Window dialogWindow = bgSetDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (getActivity().getWindowManager().getDefaultDisplay().getWidth());
        lp.height = (int) (getActivity().getWindowManager().getDefaultDisplay().getWidth() * 0.3);
        dialogWindow.setAttributes(lp); //将属性设置给窗体
        bgSetDialog.setCancelable(true);
        bgSetDialog.show();//显示对话框
    }



    /***
     * 网络请求梯户数据字典商圈集合
     */
    public void getDiction() {
        OkHttpUtils.postString()
                .url(ApiConfig.FLOOR_HOUSE_TRADIONG_AREA)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(new JSONObject().toString())
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(getContext(),response)){
                            JSONObject data = JSON.parseObject(response.get("data").toString());
                            //梯数据字典
                            floordic = JSON.parseObject(data.getJSONArray("ti").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            //户数据字典
                            housedic = JSON.parseObject(data.getJSONArray("hu").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });

                            JSONArray bussinessList = data.getJSONArray("bussinessList");

                            tradingAreaBeanslist = JSON.parseArray(bussinessList.toString(), TradingAreaBean.class);


                        }
                    }
                });



        OkHttpUtils.postString()
                .url(ApiConfig.ADMINSTRATIVE_REGION)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(new JSONObject().toString())
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(getContext(),response)) {
                            JSONArray dataList = (JSONArray) response.get("data");
                            areaBeanList = JSON.parseArray(dataList.toString(),AreaBean.class);
                        }
                    }
                });
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (data != null && requestCode == HOUSE_ADDRESS) {
                ArrayList<String> list = data.getStringArrayListExtra(ImageSelector.SELECT_RESULT);
                String url = list.get(0);
                binding.uploadHouseAddress.selectSuccess(url);
            }

            if (data != null && requestCode == LOUDONG_NUMBER) {
                ArrayList<String> list = data.getStringArrayListExtra(ImageSelector.SELECT_RESULT);
                String url = list.get(0);
                binding.uploadLoudongNumber.selectSuccess(url);
            }


            if (data != null && requestCode == DANYUAN_NUMBER) {
                ArrayList<String> list = data.getStringArrayListExtra(ImageSelector.SELECT_RESULT);
                String url = list.get(0);
                binding.uploadDanyuanNumber.selectSuccess(url);
            }


            if (data != null && requestCode == FANGMEN_NUMBER) {
                ArrayList<String> list = data.getStringArrayListExtra(ImageSelector.SELECT_RESULT);
                String url = list.get(0);
                binding.uploadFangmennumber.selectSuccess(url);
            }


            if (data != null && requestCode == HOUSE_NUMBER_ALL) {
                ArrayList<String> list = data.getStringArrayListExtra(ImageSelector.SELECT_RESULT);
                String url = list.get(0);
                binding.uploadAll.selectSuccess(url);
            }

            if (data != null && requestCode == LOUPAN_NAME) {
                ArrayList<String> list = data.getStringArrayListExtra(ImageSelector.SELECT_RESULT);
                String url = list.get(0);
                binding.uploadLoupanName.selectSuccess(url);
            }
        }
    }


    /***
     * 有房本提交事件
     */
    public void btHaveOnclick(View view){
        jsonObject.put("type",2);//申請类型
        jsonObject.put("isProperty",1);//是否有房本
        jsonObject.put("isActual",isActual);//是否跃层
        if (isActual==1){
            //添加跃层
            jsonObject.put("actualFloorFirst",yStart);
            jsonObject.put("actualFloorSecond",yEnd);
        }
        jsonObject.put("reName",binding.etHouseName.getText().toString());//楼盘名称
        if (binding.tvShangquan.getTag()!=null){
            jsonObject.put("businessId",Integer.parseInt(binding.tvShangquan.getTag().toString()));//商圈id
        }
        jsonObject.put("business",binding.tvShangquan.getText().toString());//商圈名称
        jsonObject.put("county",binding.tvXingzhengqu.getText().toString());//区县
        jsonObject.put("area",binding.tvPianjie.getText().toString());//片区
        jsonObject.put("address",binding.etAddress.getText().toString());//详细地址
        jsonObject.put("buildingNo",binding.etLoudongnumber.getText().toString());//座栋名称
        if (!TextUtils.isEmpty(binding.etLoudongDynumber.getText().toString())){jsonObject.put("unitTotal",Integer.parseInt(binding.etLoudongDynumber.getText().toString()));}//单元总数
        jsonObject.put("unitNo",binding.etDanyuannumber.getText().toString());//单元号
        jsonObject.put("roomNo",binding.etHousenumber.getText().toString());//房号
        jsonObject.put("tabFloor",binding.etFloor.getText().toString());//所在楼层
        if (!TextUtils.isEmpty(binding.etFloorAll.getText().toString())){jsonObject.put("floorTotal",Integer.parseInt(binding.etFloorAll.getText().toString()));}//楼层总数
        jsonObject.put("propertyImage",binding.uploadHouseAddress.getServerUrl());

        if (TextUtils.isEmpty(binding.etHouseName.getText().toString())){
            CustomToast.showToast(getContext(), "请输入楼盘名称", 1500);
        }else if (TextUtils.isEmpty(binding.tvShangquan.getText().toString())){
            CustomToast.showToast(getContext(), "请选择所属商圈", 1500);
        }else if (TextUtils.isEmpty(binding.tvXingzhengqu.getText().toString())){
            CustomToast.showToast(getContext(), "请选择行政区", 1500);
        }else if (TextUtils.isEmpty(binding.tvPianjie.getText().toString())){
            CustomToast.showToast(getContext(), "请选片区/街道", 1500);
        }else if (TextUtils.isEmpty(binding.etAddress.getText().toString())){
        CustomToast.showToast(getContext(), "请输入详细地址", 1500);
        }else if (TextUtils.isEmpty(binding.etLoudongnumber.getText().toString())){
            CustomToast.showToast(getContext(), "请输入所在楼栋", 1500);
        }else if (TextUtils.isEmpty(binding.etDanyuannumber.getText().toString())){
            CustomToast.showToast(getContext(), "请输入所在单元", 1500);
        }else if (TextUtils.isEmpty(binding.etHousenumber.getText().toString())){
            CustomToast.showToast(getContext(), "请输入所在房号", 1500);
        }else if (TextUtils.isEmpty(binding.etFloor.getText().toString())){
            CustomToast.showToast(getContext(), "请输入所在楼层", 1500);
        }else if (TextUtils.isEmpty(binding.etFloorAll.getText().toString())){
            CustomToast.showToast(getContext(), "请输入总楼层", 1500);
        }else if (TextUtils.isEmpty(binding.tvYuefloor.getText().toString())){
            CustomToast.showToast(getContext(), "请输入跃层信息", 1500);
        }else if (TextUtils.isEmpty(binding.tvLaddeHousehold.getText().toString())){
            CustomToast.showToast(getContext(), "请选择梯户", 1500);
        }else if (TextUtils.isEmpty(jsonObject.getString("propertyImage"))) {
            CustomToast.showToast(getContext(), "请上传房屋地址照片", 1500);
        }else{
            submit();
        }

        }


    /***
     * 无房本点击事件
     */
    public void btHavenotOnclick(View view){
        jsonObject.put("type",2);//申請类型
        jsonObject.put("isProperty",2);//是否有房本
        jsonObject.put("isActual",isActual);//是否跃层
        if (isActual==1){
            //添加跃层
            jsonObject.put("actualFloorFirst",yStart);
            jsonObject.put("actualFloorSecond",yEnd);
        }
        jsonObject.put("reName",binding.etHouseName.getText().toString());//楼盘名称
        if (binding.tvShangquan.getTag()!=null){
            jsonObject.put("businessId",Integer.parseInt(binding.tvShangquan.getTag().toString()));//商圈id
        }
        jsonObject.put("business",binding.tvShangquan.getText().toString());//商圈名称
        jsonObject.put("county",binding.tvXingzhengqu.getText().toString());//区县
        jsonObject.put("area",binding.tvPianjie.getText().toString());//片区
        jsonObject.put("address",binding.etAddress.getText().toString());//详细地址
        jsonObject.put("buildingNo",binding.etLoudongnumber.getText().toString());//座栋名称
        if (!TextUtils.isEmpty(binding.etLoudongDynumber.getText().toString())){jsonObject.put("unitTotal",Integer.parseInt(binding.etLoudongDynumber.getText().toString()));}//单元总数
        jsonObject.put("unitNo",binding.etDanyuannumber.getText().toString());//单元号
        jsonObject.put("roomNo",binding.etHousenumber.getText().toString());//房号
        jsonObject.put("tabFloor",binding.etFloor.getText().toString());//所在楼层
        if (!TextUtils.isEmpty(binding.etFloorAll.getText().toString())){jsonObject.put("floorTotal",Integer.parseInt(binding.etFloorAll.getText().toString()));}//楼层总数
        jsonObject.put("reImage",binding.uploadLoupanName.getServerUrl());
        jsonObject.put("buildingImage",binding.uploadLoudongNumber.getServerUrl());
        jsonObject.put("unitImage",binding.uploadDanyuanNumber.getServerUrl());
        jsonObject.put("roomImage",binding.uploadFangmennumber.getServerUrl());
        jsonObject.put("mailboxImage",binding.uploadAll.getServerUrl());

        if (TextUtils.isEmpty(binding.etHouseName.getText().toString())){
            CustomToast.showToast(getContext(), "请输入楼盘名称", 1500);
        }else if (TextUtils.isEmpty(binding.tvShangquan.getText().toString())){
            CustomToast.showToast(getContext(), "请选择所属商圈", 1500);
        }else if (TextUtils.isEmpty(binding.tvXingzhengqu.getText().toString())){
            CustomToast.showToast(getContext(), "请选择行政区", 1500);
        }else if (TextUtils.isEmpty(binding.tvPianjie.getText().toString())){
            CustomToast.showToast(getContext(), "请选片区/街道", 1500);
        }else if (TextUtils.isEmpty(binding.etAddress.getText().toString())){
            CustomToast.showToast(getContext(), "请输入详细地址", 1500);
        }else if (TextUtils.isEmpty(binding.etLoudongnumber.getText().toString())){
            CustomToast.showToast(getContext(), "请输入所在楼栋", 1500);
        }else if (TextUtils.isEmpty(binding.etDanyuannumber.getText().toString())){
            CustomToast.showToast(getContext(), "请输入所在单元", 1500);
        }else if (TextUtils.isEmpty(binding.etHousenumber.getText().toString())){
            CustomToast.showToast(getContext(), "请输入所在房号", 1500);
        }else if (TextUtils.isEmpty(binding.etFloor.getText().toString())){
            CustomToast.showToast(getContext(), "请输入所在楼层", 1500);
        }else if (TextUtils.isEmpty(binding.etFloorAll.getText().toString())){
            CustomToast.showToast(getContext(), "请输入总楼层", 1500);
        }else if (TextUtils.isEmpty(binding.tvYuefloor.getText().toString())){
            CustomToast.showToast(getContext(), "请输入跃层信息", 1500);
        }else if (TextUtils.isEmpty(binding.tvLaddeHousehold.getText().toString())){
            CustomToast.showToast(getContext(), "请选择梯户", 1500);
        }else if (TextUtils.isEmpty(jsonObject.getString("reImage"))) {
            CustomToast.showToast(getContext(), "请上传楼盘名称照片", 1500);
        }else if (TextUtils.isEmpty(jsonObject.getString("buildingImage"))) {
            CustomToast.showToast(getContext(), "请上传楼栋号照片", 1500);
        }else if (TextUtils.isEmpty(jsonObject.getString("unitImage"))) {
            CustomToast.showToast(getContext(), "请上传单元号照片", 1500);
        }else{
            submit();
        }

    }

    /***
     * 保存信息请求
     */
    private void submit() {
        progressDialog.show();
        OkHttpUtils.postString()
                .url(ApiConfig.SAVE_LOUPAN)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(jsonObject.toString())
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        if (null != progressDialog && progressDialog.isShowing()) {
                            progressDialog.dismiss();
                        }
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (null != progressDialog && progressDialog.isShowing()) {
                            progressDialog.dismiss();
                        }
                        if (DataFilter.filter(getContext(), response)) {
                            IconToast.showToast(getContext(), R.mipmap.ic_success, "提交成功", 1500);
                            Intent intent = new Intent(getContext(), MainActivity.class);
                            getContext().startActivity(intent);
                        }
                    }
                });
    }

    /***
     * 图片上传
     */
    private class InnerListener implements UploadImageView.Listener{
        private Fragment fragment;
        private int code;

        public InnerListener(Fragment fragment, int code) {
            this.fragment = fragment;
            this.code = code;
        }

        @Override
        public void onDeleteImage(UploadImageView view) {}

        @Override
        public void onChooseImage(UploadImageView view) {
            ImageSelector.builder()
                    .useCamera(true)
                    .setSingle(true)
                    .setViewImage(true)
                    .start(fragment, code);
        }

        @Override
        public void onPreviewImage(UploadImageView view, String previewUrl) {

            ArrayList<String> imgList = new ArrayList<>();
            ArrayList<String> nameList = new ArrayList<>();
            String url;
            if (binding.rbHave.isChecked()){
                //region Picture 1
                url = binding.uploadHouseAddress.getServerUrl();
                if (TextUtils.isEmpty(url)){
                    url = binding.uploadHouseAddress.getOriginalUrl();
                }else {
                    url = ApiConfig.MAGEHEADER_URL()+url;
                }
                if (!TextUtils.isEmpty(url)){
                    imgList.add(url);
                    nameList.add("房屋地址照片");
                }
            }else{
                url = binding.uploadLoupanName.getServerUrl();
                if (TextUtils.isEmpty(url)){
                    url = binding.uploadLoupanName.getOriginalUrl();
                }else {
                    url = ApiConfig.MAGEHEADER_URL()+url;
                }
                if (!TextUtils.isEmpty(url)){
                    imgList.add(url);
                    nameList.add("楼盘名称照片");
                }


                url = binding.uploadLoudongNumber.getServerUrl();
                if (TextUtils.isEmpty(url)){
                    url = binding.uploadLoudongNumber.getOriginalUrl();
                }else {
                    url = ApiConfig.MAGEHEADER_URL()+url;
                }
                if (!TextUtils.isEmpty(url)){
                    imgList.add(url);
                    nameList.add("楼栋号照片");
                }


                url = binding.uploadDanyuanNumber.getServerUrl();
                if (TextUtils.isEmpty(url)){
                    url = binding.uploadDanyuanNumber.getOriginalUrl();
                }else {
                    url = ApiConfig.MAGEHEADER_URL()+url;
                }
                if (!TextUtils.isEmpty(url)){
                    imgList.add(url);
                    nameList.add("单元号照片");
                }


                url = binding.uploadFangmennumber.getServerUrl();
                if (TextUtils.isEmpty(url)){
                    url = binding.uploadFangmennumber.getOriginalUrl();
                }else {
                    url = ApiConfig.MAGEHEADER_URL()+url;
                }
                if (!TextUtils.isEmpty(url)){
                    imgList.add(url);
                    nameList.add("房门号照片");
                }


                url = binding.uploadAll.getServerUrl();
                if (TextUtils.isEmpty(url)){
                    url = binding.uploadAll.getOriginalUrl();
                }else {
                    url = ApiConfig.MAGEHEADER_URL()+url;
                }
                if (!TextUtils.isEmpty(url)){
                    imgList.add(url);
                    nameList.add("所有单元全部房号报箱照片");
                }
            }

            //endregion


            int position = 0;
            for (int i = 0; i < imgList.size(); i++) {
                String imgUrl = imgList.get(i);
                imgUrl = imgUrl.replace(ApiConfig.MAGEHEADER_URL(),"");
                if (TextUtils.equals(previewUrl,imgUrl)){
                    position = i+1;
                    break;
                }
            }
            Intent intent = new Intent(getActivity(), ImageViewPagerActivity.class);
            intent.putStringArrayListExtra("imgs", imgList);
            intent.putStringArrayListExtra("names", nameList);
            intent.putExtra("position",position);
            intent.putExtra("check","picture");
            getContext().startActivity(intent);
        }

        @Override
        public void onUploadImage(final UploadImageView view, String originalUrl) {
            progressDialog.show();
            ArrayList<LubanZ.LubanZImage> files = new ArrayList<>();
            LubanZ.LubanZImage image = new LubanZ.LubanZImage();
            File file = new File(originalUrl);
            if (file.exists()){
                image.setOriginalFile(file);
                files.add(image);
            }
            UploadPictureOnly pictureOnly = new UploadPictureOnly(getContext());

            pictureOnly.uoloadOnlyImage(files, new UploadPictureOnly.UploadImageCallBack() {
                @Override
                public void onUploadSuccess(String fileName, String serverUrl) {
                    view.uploadSuccess(serverUrl);
                    Log.e("wwe", "onUploadSuccess: "+serverUrl);
                    if (null != progressDialog && progressDialog.isShowing()) {
                        progressDialog.dismiss();
                    }
                }

                @Override
                public void onUploadonFail(String s) {
                    CustomToast.showToast(getContext(),"上传失败,错误码："+s,3000);
                    view.uploadFailed();
                    if (null != progressDialog && progressDialog.isShowing()) {
                        progressDialog.dismiss();
                    }
                }
            });
        }
    }






}
